<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="addFn()"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="isLoading" :data="list">
              <el-table-column label="序号" width="100" type="index" :index="indexMethod" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="openAssignDialog(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              :current-page="from.page"
              :page-sizes="[ 2, 5, 10,20]"
              :page-size="from.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-dialog :title="title" :visible="showDialog" @close="closeDialog">
            <el-form ref="roleForm" :model="formData" :rules="rules" label-width="100px">
              <el-form-item label="角色名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入角色名称" />
              </el-form-item>
              <el-form-item label="角色描述">
                <el-input v-model="formData.description" placeholder="请输入角色描述" />
              </el-form-item>
            </el-form>
            <template #footer>
              <el-button @click="closeDialog">取消</el-button>
              <el-button type="primary" @click="onsubmit">确认</el-button>
            </template>
          </el-dialog>
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form :data="listll" label-width="120px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formcompany.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formcompany.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formcompany.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formcompany.remarks" type="textarea" :rows="3" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 分配权限的弹层 -->
      <el-dialog title="分配权限" :visible="showAssignDialog" @close="closeAssignDialog">
        <!-- :props="{'label':'name'}"  name要看接口 -->
        <el-tree ref="tree" node-key="id" :data="deptsList" check-strictly show-checkbox default-expand-all :props="{'label':'name'}" />
        <template #footer>
          <div style="text-align: right;">

            <el-button @click="closeAssignDialog">取消</el-button>
            <el-button type="primary" @click="onsubmitrole">确定</el-button>
          </div>
        </template>
      </el-dialog>
      <!-- current-page：当前页 -->
      <!-- page-sizes：下拉选择的页容量 -->
      <!-- page-size：当前页的页容量 -->
      <!-- layout：表示咱们分页的功能 -->
      <!-- total：表示的是总条数 -->
      <!-- @size-change：表示页容量发生变化 -->
      <!-- @current-change：表示页码变化 -->

      <!-- <Tansettinf ref="fromData" /> -->
    </div>
  </div>
</template>
<script>
// import Tansettinf from './components/adSeting.vue'
import { getSttingList, delSttingApi, addSttingApi, getROleSttingApi, editSttingApi, fenpeiROleSttingApi } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { getCompanyByIdApi } from '@/api/company'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Setting',

  data() {
    return {
      rolesList: [],
      deptsList: [],
      userId: '', // 存点击编辑拿到的id
      showAssignDialog: false,
      listll: [],
      formcompany: {
        remarks: '',
        mailbox: '',
        companyAddress: '',
        name: ''
      },
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: ['blur', 'change'] }]
      },
      showDialog: false,
      isLoading: false,
      list: [],
      total: null,
      from: {
        page: 1,
        pagesize: 10
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getSttingListFn()
    this.getCompanyByIdFn()
  },
  methods: {
    async onsubmitrole() {
      // 获取勾选的权限信息
      // console.log(this.$refs.tree.getCheckedNodes())
      // 分配权限首先给确定按钮绑定点击事件
      // 看接口封装获取分配角色权限信息，需要穿的参数
    // 获取需要传递的参数，首先通过this.$refs.tree.getCheckedKeys()这个方法拿到选中的选择权限的id
    // 再拿到点击需要分配角色人的id
    // 拿到之后同意存储起来
    // 调用接口的时候吧信息传入
    // 进行分配成功的提示，关闭弹框
      const treeId = this.$refs.tree.getCheckedKeys()
      const params = {
        permIds: treeId,
        id: this.userId
      }
      await fenpeiROleSttingApi(params)
      this.$message.success('分配权限成功')
      this.showAssignDialog = false
    },

    // 打开弹窗获取列表，处理成树型结构
    async openFn() {

    },
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    // 分配权限
    async openAssignDialog(id) {
      // 点击分配权限拿到id
      // 弹框显示
      // 把拿到当前行的id存储起来，因为后面方便分配权限的时候使用
      this.showAssignDialog = true
      this.userId = id
      const res = await getPermissionList()
      console.log(res)
      this.deptsList = transListToTreeData(res.data, '0')
      console.log(this.deptsList)
      const res2 = await getROleSttingApi(id)
      this.rolesList = res2.data.permIds
      console.log(this.rolesList, '444444')
      this.$refs.tree.setCheckedKeys(this.rolesList)// 赋值id进行回显
      console.log(res2, '4141')
    },
    // 公司信息数据
    async getCompanyByIdFn() {
      // 拿到vuex里面存储的数据
      const companyId = this.$store.state.user.userInfo.companyId
      const res = await getCompanyByIdApi(companyId)
      // this.listll = res.data
      this.formcompany = res.data
      console.log(res, '7777')
    },

    // 新增
    // addFn() {
    //   console.log('111')

    // },
    // 修改事件
    async editFn(id) {
      this.showDialog = true
      const res = await getROleSttingApi(id)
      console.log(res)
      this.formData = res.data
    },
    // 关闭并且清空
    closeDialog() {
      this.showDialog = false
      // this.fromData.name = ''
      this.$refs.roleForm.resetFields()
      this.formData.description = ''
      // this.formData = {
      //   name: '',
      //   description: ''
      // }
    },
    clear() {

    },
    async onsubmit() {
      this.$refs.roleForm.validate(async(flag) => {
        if (!flag) return
        if (this.formData.id) {
          console.log(this.formData.id)
          await editSttingApi(this.formData)
          this.$message.success('编辑成功')
        } else {
          console.log(this.formData.id, '222')
          await addSttingApi(this.formData)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.getSttingListFn()
      })
    },
    addFn() {
      this.showDialog = true
    },
    // 对序号进行计算
    // index传的是行号，从0开始
    // 第五页是前四页的总条数+index+1
    // 4是当前页减去1
    indexMethod(index) {
      return this.from.pagesize * (this.from.page - 1) + index + 1
    },
    // 获取列表数据
    async getSttingListFn() {
      this.isLoading = true
      // const { data: { rows, total }} = await getSttingList(this.from)
      const res = await getSttingList(this.from)
      this.list = res.data.rows
      this.total = res.data.total
      this.isLoading = false
      // this.list = rows
      // this.total = total
      // console.log(res, '11111')
    },
    // 删除操作
    async delFn(id) {
      this.$confirm('你确定要删除吗', '温馨提示').then(async() => {
        const res = await delSttingApi(id)
        this.$message.success('删除成功')
        this.getSttingListFn()
        console.log(res)
      }).catch(() => {

      })
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.from.pagesize = val
      this.getSttingListFn()
    },
    handleCurrentChange(val) {
      console.log(val)
      console.log(`当前页: ${val}`)
      this.from.page = val
      console.log(this.from.page, '2222')
      this.getSttingListFn()
    }
  }
}
</script>

<style>

</style>
